package com.example.tool.actions;

import com.example.tool.entity.ConfigEntity;
import com.example.tool.module.ModuleInformation;
import com.example.tool.ui.HToolIcon;
import com.example.tool.utils.ActionToolUtil;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

public class OpenDirAction extends AnAction {
  private static final Logger LOG = Logger.getInstance(OpenDirAction.class);

  public OpenDirAction() {
    super(ModuleInformation.OPEN_FILE, null, HToolIcon.OPEN_FILE);
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    try {
      ConfigEntity configEntity = ActionToolUtil.getInformation(project);
      if (configEntity != null) {
        String projectPath = configEntity.getProjectPath();
        String property = System.getProperty("os.name");
        if (property.contains("Windows")) {
          Runtime.getRuntime().exec("explorer.exe /e /select," + projectPath);
        } else {
          Runtime.getRuntime().exec("open " + projectPath);
        }
      } else {
        ActionToolUtil.noConfig(project);
      }
    } catch (Exception ex) {
      String text = getTemplatePresentation().getText();
      String textParse = ModuleInformation.textParse(ModuleInformation.OPEN_ACTION_MSG, text);
      LOG.info(textParse + ex.getMessage());
      ActionToolUtil.showNotification(project, NotificationType.ERROR, textParse);
    }
  }
}
